const g = require('../../lib/global');

cc.Class({
    extends: cc.Component,

    properties: {
        // 投注按钮
        betBtn: {
            type: cc.Button,
            default: null,
        },
        // 10k筹码
        chip10k: {
            type: cc.Button,
            default: null,
        },
        // 5k筹码
        chip5k: {
            type: cc.Button,
            default: null
        },
        // 2k筹码
        chip2k: {
            type: cc.Button,
            default: null
        },
        // 1k筹码
        chip1k: {
            type: cc.Button,
            default: null
        },
        // 重新下注
        resetBtn: {
            type: cc.Button,
            default: null,
        },
        // 开始下注
        startBetBtn: {
            type: cc.Button,
            default: null,
        },
        // 投掷点
        anchorChipToss: {
            type: cc.Node,
            default: null,
        },
        // 金币预制体
        chipPrefab: cc.Prefab,
        // 投注按钮
        betUI: cc.Node,
        // 提示文本
        resultText2: cc.Label,
    },

    // 注册控制器
    initWithController: function(controller) {
        this.controller = controller;
        // 隐藏下注按钮
        this.startBetBtn.node.active = false;
    },

    onLoad: function() {
        this.register();
        this.resultText.string = "请下注";
    },

    // 注册事件
    register: function () {
        const chips = [this.chip10k, this.chip5k, this.chip2k, this.chip1k];
        const betType = [g.CHIP_TYPE.ten, g.CHIP_TYPE.five, g.CHIP_TYPE.two, g.CHIP_TYPE.one];
        for(let i = 0; i < chips.length; i++) {
            const btn = chips[i];
            btn.node.on(cc.Node.EventType.TOUCH_START, function (event) {
                this.controller.bet(betType[i])
            }, this)
        }
    },

    // 播放动画
    playAddChip: function () {
        // 让硬币落在圆内
        const startPos = cc.v2((Math.random() - 0.5)*100, (Math.random() - 0.5)*100);
        const chip = cc.instantiate(this.chipPrefab);
        this.anchorChipToss.addChild(chip);
        chip.setPosition(startPos);
        chip.getComponent("chipView").play();
    },

    // 是否显示投注按钮
    showBetBtn: function (show) {
        this.betUI.enabled = show;
    },

    // 开始投注
    startBet: function (event, customData) {
        cc.log(`start bet custom data = ${JSON.stringify(customData)}, fsm start bet`);
        this.controller.fsm.toDeal();
    }

});
